package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.CodeUtil;
import com.wing.enterprisecab.cabinet.response.InventoryAlarmDetailVO;
import com.wing.enterprisecab.feign.EnterpriseCabClient;
import com.wing.product.dao.DynamicDao;
import com.wing.purchase.dao.ReplenishmentOrderDao;
import com.wing.purchase.model.entity.ReplenishmentOrder;
import com.wing.purchase.model.entity.ReplenishmentOrderDetail;
import com.wing.purchase.model.request.ReplenishmentOrderForm;
import com.wing.purchase.model.response.ReplenishmentOrderVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 补货订单表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ReplenishmentOrderServiceImpl extends ServiceImpl<ReplenishmentOrderDao, ReplenishmentOrder> implements ReplenishmentOrderService {

    @Autowired
    private EnterpriseCabClient enterpriseCabClient;
    @Autowired
    private ReplenishmentOrderDetailService replenishmentOrderDetailService;
    @Autowired
    private DynamicDao dynamicDao;
    @Override
    public int add(ReplenishmentOrderForm replenishmentOrderForm) {

        //校验

        ReplenishmentOrder replenishmentOrder =new ReplenishmentOrder();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ReplenishmentOrder.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(replenishmentOrderForm, replenishmentOrder, copyOptions);

        return baseMapper.insert(replenishmentOrder);
    }

    @Override
    public List<ReplenishmentOrderVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ReplenishmentOrderVO> pageList4app(Page page, Map<String, Object> params) {
        List<ReplenishmentOrderVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ReplenishmentOrderVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
    @Transactional
    @Override
    public void submitReplenishmentOrder(List<ReplenishmentOrderForm> replenishmentOrderFormList) {
        if(CollectionUtils.isNotEmpty(replenishmentOrderFormList)){
            ReplenishmentOrder orderForm = new ReplenishmentOrder();
            String today = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
            Integer maxNo = dynamicDao.getDayMaxNo("ord_replenishment_order");
            String code = CodeUtil.getCode(maxNo);
            orderForm.setCode("BH" + today +  code);
            orderForm.setNo(Integer.valueOf(code));
            orderForm.setDay(Integer.valueOf(today));
            orderForm.setEnterpriseCode(JwtUtils.getEnterpriseCode());
            this.save(orderForm);
            for (ReplenishmentOrderForm item : replenishmentOrderFormList){
                ReplenishmentOrderDetail orderDetail = new ReplenishmentOrderDetail();
                orderDetail.setReplenishmentOrderId(orderForm.getId())
                        .setSkuCode(item.getSkuCode())
                        .setProductName(item.getSkuName())
                        .setCabCode(item.getCabCode())
                        .setCurentStock(item.getQuantity())
                        .setAlarmStock(item.getQuantitySetting())
                        .setCabCode(item.getBelongCabinet())
                        .setInventoryAlarmId(item.getInventoryAlarmId())
                        .setEnterpriseCode(JwtUtils.getEnterpriseCode());
                replenishmentOrderDetailService.save(orderDetail);

            }
        }

    }
}
